import { Body, Param, Controller, Get, Post, Patch, Query, Put } from '@nestjs/common';
import AffiliateService from '../service/AffiliateService';
import * as AffiliateReq from '../model/req/AffiliateReq';
import { ApiTags, ApiOperation, ApiParam, ApiQuery, ApiHeader } from '@nestjs/swagger';
import { Page, results, Err } from '../common/back';
import { validate } from 'class-validator';
import { plainToClass } from 'class-transformer';

@ApiTags('会员列表')
@Controller("affiliate")
export default class UserController {
    constructor(private readonly affiliateService: AffiliateService<any>) { }

    @ApiOperation({ summary: '根据会员id查询会员信息', description: '根据会员id查询会员信息' })
    @Post('getAffiliate')
    async getAffiliate(@Body() body: AffiliateReq.setAffiliateReq) {
        const Request = plainToClass(AffiliateReq.setAffiliateReq, body);
        const errors = await validate(Request);
        if (errors.length > 0) {
            return new Err(errors[0].constraints!.isNotEmpty, 400);
        }

        try {
            const data = await this.affiliateService.getAffiliate(body.affiliate_id);
            if (typeof data == "string") {
                return new Err(data, 204)
            }
            return new results(data);
        } catch (err: any) {
            return new Err(err.sqlMessage || '服务器错误', 500);
        }
    }

    @ApiOperation({ summary: '根据会员id关闭会员', description: '根据会员id关闭会员' })
    @Post('setAffiliateState')
    async setAffiliateState(@Body() body: AffiliateReq.setAffiliateReq) {
        const Request = plainToClass(AffiliateReq.setAffiliateReq, body);
        const errors = await validate(Request);
        if (errors.length > 0) {
            return new Err(errors[0].constraints!.isNotEmpty, 400);
        }

        try {
            const data = await this.affiliateService.setAffiliateState(body);
            if (typeof data == "string") {
                return new Err(data, 204)
            }
            return new results(data);
        } catch (err: any) {
            return new Err(err.sqlMessage || '服务器错误', 500);
        }
    }

    @ApiOperation({ summary: '根据会员id删除会员', description: '根据会员id删除会员' })
    @Post('delAffiliateState')
    async delAffiliateState(@Body() body: AffiliateReq.setAffiliateReq) {
        const Request = plainToClass(AffiliateReq.setAffiliateReq, body);
        const errors = await validate(Request);
        if (errors.length > 0) {
            return new Err(errors[0].constraints!.isNotEmpty, 400);
        }

        try {
            const data = await this.affiliateService.delAffiliate(body);
            if (typeof data == "string") {
                return new Err(data, 204)
            }
            return new results("删除成功");
        } catch (err: any) {
            return new Err(err.sqlMessage || '服务器错误', 500);
        }
    }

    @ApiOperation({ summary: '添加会员', description: '添加会员' })
    @Post('addAffiliate')
    async addAffiliate(@Body() body: any) {
        try {
            this.affiliateService.addAffiliate(body);
        } catch (err: any) {
            return new Err(err.sqlMessage || '服务器错误', 500);
        }
    }


}
